package www.openteam.com.model.policy;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.List;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.CustomOrder;
import www.openteam.com.bean.OrderApproval;
import www.openteam.com.bean.UserSpRight;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2017/1/9.
 */

public class CustomOrderApprovalModel extends BaseModel {

    private boolean isQuery = false;

    private boolean isApproval = false;

    private boolean isUnApproval =false;

    private boolean isCheck =false;

    //void queryOrder(String custorderid, String custid, String orderStatus,
    // long startTime, long endTime, int PageSize);

    public boolean queryOrder(@NonNull String custorderid,
                               @NonNull final String custid,
                               @NonNull String orderStatus,
                               long startDate,
                               long endDate,
                              String stype, String moreCom,
                              int userId,String jMark,
                               @NonNull int PageSize, @NonNull final
                               CustomOrderApprovalOnListener customOrderApprovalOnListener) {
        httpApiService.queryOrder(custorderid, custid, orderStatus, startDate, endDate, stype,moreCom,userId,jMark, PageSize)
                .compose(new CommonTransformer<OrderApproval>())
                .subscribe(new CommonSubscriber<OrderApproval>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(OrderApproval orderApproval) {
                        isQuery = true;
                        customOrderApprovalOnListener.queryCOASuccess(orderApproval.getData());
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        customOrderApprovalOnListener.queryCOAFail(e.message);
                    }
                });
        return isQuery;
    }


    public boolean approvalStatu(@NonNull int id, @NonNull final CustomOrderAOnListener customOrderAOnListener) {
            httpApiService.approvalStatu(id)
                    .compose(new CommonTransformer<ApprovalStatu>())
                    .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                        @Override
                        public void onNext(ApprovalStatu approvalStatu) {
                                isApproval =true;
                            customOrderAOnListener.approvalCOASuccess(approvalStatu);
                        }

                        @Override
                        protected void onError(ApiException e) {
                            super.onError(e);
                            isApproval= false;
                            customOrderAOnListener.approvalCOAFail(e.message);
                        }
                    });
        return  isApproval;
    }


    public boolean unapprovalStatu(@NonNull int id, @NonNull final CustomOrderUOnListener customOrderUOnListener) {
        httpApiService.unapprovalStatu(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isUnApproval =true;
                        customOrderUOnListener.unApprovalCOASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isUnApproval= false;
                        customOrderUOnListener.unApprovalCOAFail(e.message);
                    }
                });
        return  isUnApproval;
    }


    public  boolean checkIsLAM(@NonNull int uId,
                                @NonNull  int rId,
                                @NonNull final CustomOrderILAMListener customOrderILAMListener){

                httpApiService.checkIsLAM(uId, rId)
                        .compose(new CommonTransformer<UserSpRight>())
                        .subscribe(new CommonSubscriber<UserSpRight>(OTApplication.getmContext()) {
                            @Override
                            public void onNext(UserSpRight userSpRight) {
                                isCheck= true;
                                customOrderILAMListener.checkILAMSuccess(userSpRight);
                            }

                            @Override
                            protected void onError(ApiException e) {
                                super.onError(e);
                                isCheck=false;
                                customOrderILAMListener.checkILAMFail(e.message);
                            }
                        });
        return isCheck;

    }

    //客户订单详细
    public interface CustomOrderApprovalOnListener {
        void queryCOASuccess(List<OrderApproval.DataEntity> orderApprovals);

        void queryCOAFail(String failMsg);
    }

    //客户订单审核
    public interface  CustomOrderAOnListener{
        void approvalCOASuccess(ApprovalStatu aprovalStatu);
        void approvalCOAFail(String failMsg);

    }
    //客户订单反审核
    public interface  CustomOrderUOnListener{
        void unApprovalCOASuccess(ApprovalStatu aprovalStatu);
        void unApprovalCOAFail(String failMsg);
    }

    //客户订单金额
    public interface CustomOrderILAMListener{
        void checkILAMSuccess(UserSpRight userSpRight);
        void checkILAMFail(String failMsg);
    }
}
